﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using SobFS.IDAL;
using SobFS.Common.Database;

namespace SobFS.MySQLDAL
{
	/// <summary>
	/// 数据访问类:SysOrganization
	/// </summary>
    public partial class SysOrganization : ISysOrganization
	{
		public SysOrganization()
		{}
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("Id", "SysOrganization");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SysOrganization");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SobFS.Model.SysOrganization model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SysOrganization(");
            strSql.Append("ParentId,Code,Name,Position,IsObsoleted,IsEnabled,Description)");
            strSql.Append(" values (");
            strSql.Append("@ParentId,@Code,@Name,@Position,@IsObsoleted,@IsEnabled,@Description)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@Code", MySqlDbType.VarChar,100),
					new MySqlParameter("@Name", MySqlDbType.VarChar,100),
					new MySqlParameter("@Position", MySqlDbType.Int32,11),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Description", MySqlDbType.Text)};
            parameters[0].Value = model.ParentId;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Position;
            parameters[4].Value = model.IsObsoleted;
            parameters[5].Value = model.IsEnabled;
            parameters[6].Value = model.Description;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SobFS.Model.SysOrganization model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SysOrganization set ");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("Code=@Code,");
            strSql.Append("Name=@Name,");
            strSql.Append("Position=@Position,");
            strSql.Append("IsObsoleted=@IsObsoleted,");
            strSql.Append("IsEnabled=@IsEnabled,");
            strSql.Append("Description=@Description");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ParentId", MySqlDbType.Int32,11),
					new MySqlParameter("@Code", MySqlDbType.VarChar,100),
					new MySqlParameter("@Name", MySqlDbType.VarChar,100),
					new MySqlParameter("@Position", MySqlDbType.Int32,11),
					new MySqlParameter("@IsObsoleted", MySqlDbType.Bit),
					new MySqlParameter("@IsEnabled", MySqlDbType.Bit),
					new MySqlParameter("@Description", MySqlDbType.Text),
					new MySqlParameter("@Id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.ParentId;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Position;
            parameters[4].Value = model.IsObsoleted;
            parameters[5].Value = model.IsEnabled;
            parameters[6].Value = model.Description;
            parameters[7].Value = model.Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysOrganization ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysOrganization ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SobFS.Model.SysOrganization GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId,Code,Name,Position,IsObsoleted,IsEnabled,Description from SysOrganization ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Id", MySqlDbType.Int32)
			};
            parameters[0].Value = Id;

            SobFS.Model.SysOrganization model = new SobFS.Model.SysOrganization();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentId"] != null && ds.Tables[0].Rows[0]["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(ds.Tables[0].Rows[0]["ParentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Code"] != null && ds.Tables[0].Rows[0]["Code"].ToString() != "")
                {
                    model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Position"] != null && ds.Tables[0].Rows[0]["Position"].ToString() != "")
                {
                    model.Position = int.Parse(ds.Tables[0].Rows[0]["Position"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsObsoleted"] != null && ds.Tables[0].Rows[0]["IsObsoleted"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsObsoleted"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsObsoleted"].ToString().ToLower() == "true"))
                    {
                        model.IsObsoleted = true;
                    }
                    else
                    {
                        model.IsObsoleted = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["IsEnabled"] != null && ds.Tables[0].Rows[0]["IsEnabled"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsEnabled"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsEnabled"].ToString().ToLower() == "true"))
                    {
                        model.IsEnabled = true;
                    }
                    else
                    {
                        model.IsEnabled = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Description"] != null && ds.Tables[0].Rows[0]["Description"].ToString() != "")
                {
                    model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId,Code,Name,Position,IsObsoleted,IsEnabled,Description ");
            strSql.Append(" FROM SysOrganization ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SysOrganization ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from SysOrganization T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}

